<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/1/27
 * Time: 12:30 PM
 */

namespace api\models;


use common\libs\Constants;

class Comment extends \common\models\Comment
{
    public function fields()
    {
        return [
            'id',
            'aid',
            'uid',
            'user_avatar' => function ($model) {
                return $model->user->getCurrentAvatar();
            },
            'user_name'   => function ($model) {
                return $model->user->username;
            },
            'content',
            'created_at'  => function ($model) {
                return date("Y-m-d H:i:s", $model->created_at);
            }
        ];
    }

    public function rules()
    {
        return [
            [['aid', 'uid', 'reply_to', 'status', 'created_at', 'updated_at'], 'integer'],
            [['content'], 'required'],
            [['content', 'nickname', 'email', 'website_url'], 'string'],
            [['ip'], 'string', 'max' => 255],
        ];
    }

    /**
     * @param $id
     *
     * @return array
     */
    public function getCommentByAid($id)
    {
        $list = self::find()
                    ->where(['aid' => $id, 'status' => self::STATUS_PASSED])
                    ->orderBy("id desc,reply_to desc")
                    ->all();

        return $list;
    }


    public function afterFind()
    {
        parent::afterFind();
    }

    public function beforeSave($insert)
    {
        $this->status = Comment::STATUS_PASSED;
        $this->uid    = \Yii::$app->getUser()->id;

        return parent::beforeSave($insert);
    }
}